﻿using AutoMapper;
using Sodao.Demo.Application;
using Sodao.Demo.Core.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sodao.Demo.Service
{
    public class Startup
    {
        public static readonly AutoFacManage _autofacManageProvide = new AutoFacManage();


        public static AutoFacManage GetInstance
        {
            get
            {
                if (_autofacManageProvide == null)
                {
                    return new AutoFacManage();
                }
                return _autofacManageProvide;
            }
        }

        public static void Init()
        {
            Startup.GetInstance.AddApplicationDI();
            Startup.GetInstance.Init();
            MapperInit();
        }

        public static void MapperInit()
        {
            Mapper.Initialize(config =>
            {
                config.CreateMap<long, DateTime>().ConvertUsing(new LongToDateTimeConverter());
                config.CreateMap<DateTime, long>().ConvertUsing(new DateTimeToLongConverter());
            });
        }

        internal class DateTimeToLongConverter : ITypeConverter<DateTime, long>
        {
            public long Convert(DateTime source, long destination, ResolutionContext context)
            {
                return source.Ticks;
            }
        }


        internal class LongToDateTimeConverter : ITypeConverter<long, DateTime>
        {
            public DateTime Convert(long source, DateTime destination, ResolutionContext context)
            {
                bool flag = source == 0L;
                DateTime result;
                if (flag)
                {
                    result = default(DateTime);
                }
                else
                {
                    result = new DateTime(source);
                }
                return result;
            }
        }
    }
}
